﻿using System.Linq;
using INSE6260.OnlineBanking.Infrastructure;
using INSE6260.OnlineBanking.Model.Accounts;
using INSE6260.OnlineBanking.Model.ClientCard;
using System;

namespace INSE6260.OnlineBanking.Repository.EF.Repositories
{
    public class ClientCardRepository : RepositoryBase<ClientCard>, IClientCardRepository
    {
        public ClientCardRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public ClientCard GetClientCard(string cardNo, string password)
        {
            return GetObjectSet().FirstOrDefault(cc => cc.CardNo == cardNo && cc.Password == password);
        }

        public ClientCard GetClientCard(string cardNo)
        {
            return GetObjectSet().FirstOrDefault(cc => cc.CardNo == cardNo);
        }

        public ClientCard GetClientCard(int accountId)
        {
            return FindByID(accountId);
        }

        public string GetRandomSecurityQuestion(ClientCard clientCard)
        {
            var random = new Random();
            var nextRandom = random.Next(1, 3);
            for (var index = 0; index < 100; index++)
            {
                random = new Random(nextRandom);
                nextRandom = random.Next(1, 3);
            }
            var question = String.Empty;
            if (nextRandom == 1) return clientCard.SecurityQuestion1;
            if (nextRandom == 2) return clientCard.SecurityQuestion2;
            return nextRandom == 3 ? clientCard.SecurityQuestion3 : question;
        }

        //public string GetRandomSecurityQuestion(ClientCard clientCard)
        //{
        //    var random = new Random().Next(1, 3);
        //    var question = String.Empty;
        //    if (random == 1) return clientCard.SecurityQuestion1;
        //    if (random == 2) return clientCard.SecurityQuestion2;
        //    return random == 3 ? clientCard.SecurityQuestion3 : question;
        //}

        public bool IsLocked(LoginInfo loginInfo)
        {
            var clientCard = GetObjectSet().FirstOrDefault(cc => cc.CardNo == loginInfo.CardNo);
            if (clientCard != null) if (clientCard.Account != null) return clientCard.Status;
            return false;
        }

        public bool ExistCardNo(LoginInfo loginInfo)
        {
            var clientCard = GetObjectSet().FirstOrDefault(cc => cc.CardNo == loginInfo.CardNo);
            if (clientCard != null) if (clientCard.Account != null) return true;
            return false;
        }
        public Account IsValid(LoginInfo loginInfo)
        {
            var clientCard = GetObjectSet().FirstOrDefault(cc => cc.CardNo == loginInfo.CardNo && cc.Password == loginInfo.Password);
            if (clientCard != null) if (clientCard.Account != null) return clientCard.Account;
            return null;
        }

        public void IsSecured (LoginInfo loginInfo)
        {
            var clientCard = GetObjectSet().FirstOrDefault(cc => cc.CardNo == loginInfo.CardNo);
            if (clientCard != null)
            {
                loginInfo.Counter = 3;
            }
        }

        public void IsSecured(SecurityInfo securityInfo)
        {
            var clientCard = GetObjectSet().FirstOrDefault(cc => cc.AccountId == securityInfo.AccountId);

            if (clientCard != null)
            {
                securityInfo.IsSecuredToken = ((clientCard.SecurityQuestion1 == securityInfo.Question) && (clientCard.SecurityAnswer1 == securityInfo.Answer)
                                              || (clientCard.SecurityQuestion2 == securityInfo.Question) && (clientCard.SecurityAnswer2 == securityInfo.Answer)
                                              || (clientCard.SecurityQuestion3 == securityInfo.Question) && (clientCard.SecurityAnswer3 == securityInfo.Answer)
                                              );

                securityInfo.ClientId = clientCard.Account.ClientId;
                securityInfo.Client = clientCard.Account.Client;
                securityInfo.CardNo = clientCard.CardNo;
                securityInfo.Password = clientCard.Password;
                securityInfo.ClientCardId = clientCard.Id;
                securityInfo.Counter = 3;
            }

        }

        public void ChangePassword(ClientCard clientCard)
        {
            Update(clientCard);
        }

        public void ChangeSecurityQuestions(ClientCard clientCard)
        {
            Update(clientCard);
        }

        public void BlockCard(int clientCardId)
        {
            var card = FindByID(clientCardId);
            card.Status = false;
            Update(card);
            SaveChanges();
        }
    }
}
